﻿Imports System.Collections.Specialized
Imports System.ComponentModel

Public Class StrokeThumbnail
    Inherits FrameworkElement

    Private Shared BackgroundBrush As Brush = New SolidColorBrush(Color.FromArgb(&H20, 0, 0, 0))

    Shared Sub New()
        BackgroundBrush.Freeze()
    End Sub

    Public Shared ReadOnly StrokesProperty As DependencyProperty = _
        DependencyProperty.Register( _
            "Strokes", GetType(StrokeCollection), GetType(StrokeThumbnail), _
            New FrameworkPropertyMetadata( _
                Nothing, _
                Sub(d, e) CType(d, StrokeThumbnail).OnStrokesChanged(CType(e.OldValue, StrokeCollection), CType(e.NewValue, StrokeCollection))))

    Public Property Strokes As StrokeCollection
        Get
            Return CType(GetValue(StrokesProperty), StrokeCollection)
        End Get
        Set(ByVal value As StrokeCollection)
            SetValue(StrokesProperty, value)
        End Set
    End Property

    Private Sub OnStrokesChanged(oldValue As StrokeCollection, newValue As StrokeCollection)
        If oldValue IsNot Nothing Then
            RemoveHandler oldValue.StrokesChanged, AddressOf StrokesChanged
        End If
        If newValue IsNot Nothing Then
            AddHandler newValue.StrokesChanged, AddressOf StrokesChanged
        End If
        StrokesChanged(Me, New EventArgs)
    End Sub

    Private Sub StrokesChanged(sender As Object, e As EventArgs)
        InvalidateVisual()
    End Sub



    Protected Overrides Sub OnRender(dc As System.Windows.Media.DrawingContext)
        MyBase.OnRender(dc)

        dc.DrawRectangle(BackgroundBrush, Nothing, New Rect(RenderSize))

        dc.PushTransform(New TranslateTransform(-GlyphMetrics.BorderOverflow, -GlyphMetrics.BorderOverflow))
        Strokes.Draw(dc)
        dc.Pop()
    End Sub

End Class
